<?php
/**
 * @version $Id: 1.0
 * @package Sigsiu Online Business Index 2
 * @authorName Sigrid & Radek Suski, Sigsiu.NET
 * @authorEmail info@sigsiu.net
 * @authorUrl http://www.sigsiu.net
 * @copyright Copyright (C) 2007 Sigsiu.NET (http://www.sigsiu.net). All rights reserved.
 * @license see http://www.gnu.org/copyleft/gpl.html GNU/GPL.
 * SOBI2 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 * payment.class.php 1-Jan-2007 Radek Suski
 */

 // no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

class payment {

	var $feesOptions = array();
	var $fees = null;
	var $total = null;
	var $sobi2Id = null;
	var $msg = "";
	var $sobi = null;
	/**
	 * default constructor
	 * @param sobi2 $Sobi
	 */
    function payment($Sobi) {
    	global $config;
    	$config->getPayment();
    	if($Sobi) {
    		$this->feesOptions = $Sobi->fees;
    		$this->sobi2Id = $Sobi->id;
    		$this->sobi = $Sobi;
    	}
 		else
 			mosRedirect( "/index.php", _SOBI2_NOT_AUTH);
    }
    /*
     * displaying all selected  not free options
     */
    function showFees() {
    	global $config, $mosConfig_live_site, $my,$database;
    	$this->msg = $this->msg."\n\t <div class='sobi2PaymentContainer'><h3>"._SOBI2_PAY_CHOSEN_OPTIONS."</h3>";
    	$this->msg = $this->msg. "\n\t  <table id='sobi2Payment'>";
		foreach($this->feesOptions as $option => $payment ) {
	    	$this->fees = $this->fees + $payment;
	    	$payment = $config->getCurrencyFormat($payment);
	    	$this->msg = $this->msg. "\n\t\t <tr> ";
	    	$this->msg = $this->msg. "\n\t\t\t <td class='sobi2PayOption'>";
	    	$this->msg = $this->msg. "\n\t\t\t {$option}";
	    	$this->msg = $this->msg. "\n\t\t\t </td>";
	    	$this->msg = $this->msg. "\n\t\t\t <td class='sobi2PayFees'>";
	    	$this->msg = $this->msg. "\n\t\t\t {$payment}";
	    	$this->msg = $this->msg. "\n\t\t\t </td>";
	    	$this->msg = $this->msg. "\n\t\t </tr> ";
		}

		$this->total = $this->fees;
		$this->fees = $config->getCurrencyFormat($this->fees);

    	$this->msg = $this->msg. "\n\t\t <tr> ";
    	$this->msg = $this->msg. "\n\t\t\t <td>";
    	$this->msg = $this->msg. "\n\t\t\t\t "._SOBI2_PAY_TOTAL_AMOUNT;
    	$this->msg = $this->msg. "\n\t\t\t </td>";
    	$this->msg = $this->msg. "\n\t\t\t <td id='sobi2PayTotal'>";
    	$this->msg = $this->msg. "\n\t\t\t\t {$this->fees}";
    	$this->msg = $this->msg. "\n\t\t\t </td>";
    	$this->msg = $this->msg. "\n\t\t </tr> ";
		$this->msg = $this->msg. "\n\t</table>";

		if($config->mailFees)
			$this->mailFees();

		echo $this->msg;
		echo "\n\t  <table id='sobi2PaymentMethod'>";
		echo "\n\t\t <tr> ";

		if($config->useBankTransfer) {
			echo "\n\t\t\t <td id='sobi2PaymentMethodBank'> ";
			if($config->useBankTransfer == 1)
				echo "\n\t\t\t\t "._SOBI2_PAY_BANK_DATA_SEND_EMAIL;
			else {
				echo "\n\t\t\t\t<b>";
				echo _SOBI2_PAY_WITH_BANK;
				echo "</b><br/>";
				echo _SOBI2_PAY_BANK_DATA_JS_HEADER;
				echo " <br/><br/> {$config->bankData} <br/>";
				echo _SOBI2_PAY_BANK_DATA_JS_TITLE." ".$config->payTitle." "._SOBI2_NUMBER_H." ".$this->sobi2Id;
				echo "<br/>";
				echo _SOBI2_PAY_TOTAL_AMOUNT." ".$this->fees;
			}
			echo "\n\t\t\t\t <br/>";
			echo "\n\t\t\t </td>";
		}
		if($config->usePayPal) {
			/*
			 * paypal accept only point
			 */
			$this->fees = str_replace(",",".",$this->fees);
			$this->total = str_replace(",",".",$this->total);
			
			$query = "SELECT title FROM #__sobi2_item WHERE itemid='$this->sobi2Id'";
                        $database->setQuery($query);

                        $row =null;
                        if ($database->loadObject($row))
                        {
                                $itemtitle = $row->title;
                        }

			$title = $config->payTitle." ".$itemtitle;
			/*$href = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&" .
					"business={$config->payPalMail}" .
					"&item_name={$title}" .
					"&item_number= {$this->sobi2Id}" .
					"&amount={$this->total}" .
					"&no_shipping=2&no_note=1" .
					"&currency_code={$config->currency}" .
					"&bn=PP%2dBuyNowBF&charset=UTF%2d8";

			*/
			/*
			$href = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick-subscriptions&" .
					"business={$config->payPalMail}" .
					"&item_name={$title}" .
					"&item_number={$this->sobi2Id}" .
					"&a3={$this->total}" .
					"&t3=Y" .
					"&p3=1" .
					"&no_shipping=2&no_note=1" .
					"&currency_code={$config->currency}" .
					"&bn=PP%2dBuyNowBF&charset=UTF%2d8";
			*/

			/* t3 - time period (D=days, W=weeks, M=months, Y=years)
			   p3 - number of time periods between each recurrence */

			//$href = str_replace(" ", "%20", $href);
			//echo "\n\t\t\t <td id='sobi2PaymentMethodPayPal'> ";
			//echo "\n\t\t\t\t <a href='{$href}' title='"._SOBI2_PAY_WITH_PAYPAL."'>";
			//echo "\n\t\t\t\t <img src='{$mosConfig_live_site}/components/com_sobi2/images/paypal.jpg' name='submit' alt='"._SOBI2_PAY_WITH_PAYPAL."'>";
			//echo "\n\t\t\t\t <br/>"._SOBI2_PAY_WITH_PAYPAL."</a>";
			//echo "\n\t\t\t </td>";
			
			$query = "SELECT sandbox FROM #__myipn_config WHERE id='1'";
                        $database->setQuery($query);

                        $row =null;
			if ($database->loadObject($row)) 
			{
				if ($row->sandbox == 1)
                               		$posturl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
				else
			       		$posturl = "https://www.paypal.com/cgi-bin/webscr";
			}
			echo "<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>OR</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
			
			echo "<td>\n\t\t\t\t<b>";
                                echo _SOBI2_PAY_WITH_PAYPAL;
                                echo "</b><br /><br /><br /><br /><br /><br />";
			$html .= "<form name='checkout' method='post' action='".$posturl."'>";
                        $html .= "<input type='hidden' name='business' value='".$config->payPalMail."'>";
                        $html .= "<input type='hidden' name='tax' value='0'>";
                        $html .= "<input type='hidden' name='no_note' value='1'>";
                        $html .= "<input type='hidden' name='no_shipping' value='1'>";
                        $html .= "<input type='hidden' name='cmd' value='_xclick'>";
                        $html .= "<input type='hidden' name='rm' value='2'>";
                        $html .= "<input type='hidden' name='custom' value=".$my->username.">";
                        $html .= "<input type='hidden' name='currency_code' value='".$config->currency."'>";
                        $html .= "<input type='hidden' name='notify_url' value='".$GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=paypal' />";
                        $html .= "<input type='hidden' name='return' value='".$GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=complete'>";
                        $html .= "<input type='hidden' name='cancel_return' value='".$GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=cancel'>";
                        $html .= "<input type='hidden' name='item_name' value='".$title."'>";

                        $html .= "<input type='hidden' name='item_number' value='".$this->sobi2Id."'>";
                        $html .= "<input type='hidden' name='amount' value='".$this->total."'>";
                        $html .= "<input type='submit' name='submit' value='Pay with Paypal' class='button'>";
                        $html .= "</form>";
                        echo $html;
			echo "</td>";

		}
		echo "\n\t\t </tr> ";
		echo "\n\t </table>\n\t </div>";


    }
    /*
     * sending emails
     */
    function mailFees() {
    	global $mosConfig_mailfrom,$mosConfig_fromname,$database,$config,$my;
		$mailMsg = "";
		if ($mosConfig_mailfrom != '' && $mosConfig_fromname != '') {
			$adminName2 	= $mosConfig_fromname;
			$adminEmail2 	= $mosConfig_mailfrom;
		} else {
		// use email address and name of first superadmin for use in email sent to user
			$query = "SELECT name, email"
			. "\n FROM #__users"
			. "\n WHERE LOWER( usertype ) = 'superadministrator'"
			. "\n OR LOWER( usertype ) = 'super administrator'"
			;
			$database->setQuery( $query );
			$rows = $database->loadObjectList();
			$row2 			= $rows[0];
			$adminName2 	= $row2->name;
			$adminEmail2 	= $row2->email;
		}
		/*
		 * getting client email
		 */
		//$query = "SELECT `data_txt` FROM `#__sobi2_fields_data` WHERE `fieldid` = 7 AND `itemid` = {$this->sobi2Id}";
		$query = "SELECT email FROM `#__users` WHERE `username` = '$my->username'";
		$database->setQuery( $query ); 
		$email = $database->loadResult();

		$config->getEmails();
		$mailMsg  = $this->replaceMarkers($config->UserEmailPaymentsText);
		$mailTitle = $this->replaceMarkers($config->UserEmailPaymentsTitle);

		$AdmMailMsg = null;
		$AdmMailTitle = null;

		if($config->mailFeesAdm){
			$AdmMailMsg = $this->replaceMarkers($config->AdmEmailPaymentsText);
			$AdmMailTitle = $this->replaceMarkers($config->AdmEmailPaymentsTitle);
		}

		if(!$email) {
			$email = $adminEmail2;
			$mailMsg = "EMAIL ERROR: \n\n".$mailMsg;
		}

		/*
		 * replacing selectet option marker
		 */
		$selected_options = null;
		foreach($this->feesOptions as $option => $payment ) {
	    	$payment = $config->getCurrencyFormat($payment);
	    	$selected_options = $selected_options. "\n {$option}";
	    	$selected_options = $selected_options. "\t\t {$payment}";
		}
		$mailMsg = str_replace("{selected_options}",$selected_options, $mailMsg);
		$AdmMailMsg = str_replace("{selected_options}",$selected_options, $AdmMailMsg);

		/*
		 * replacing PayPal url
		 */
		if($config->usePayPal) {
			//$this->fees = str_replace(",",".",$this->fees);
			$this->total = str_replace(",",".",$this->total);
			$query = "INSERT INTO #__myipn_entry_log VALUES ( '','$this->sobi2Id','$this->total','$config->currency')";
                                                $database->setQuery($query);
                                                $database->query();

			//$title = $config->payTitle." "._SOBI2_NUMBER_H." ".$this->sobi2Id;
			/*$href = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&" .
					"business={$config->payPalMail}" .
					"&item_name={$title}" .
					"&item_number= {$this->sobi2Id}" .
					"&amount={$this->total}" .
					"&no_shipping=2&no_note=1" .
					"&currency_code={$config->currency}" .
					"&bn=PP%2dBuyNowBF&charset=UTF%2d8";*/

            /*
			$href = "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick-subscriptions&" .
					"business={$config->payPalMail}" .
					"&item_name={$title}" .
					"&item_number={$this->sobi2Id}" .
					"&a3={$this->total}" .
					"&t3=Y" .
					"&p3=1" .
					"&no_shipping=2&no_note=1" .
					"&currency_code={$config->currency}" .
					"&bn=PP%2dBuyNowBF&charset=UTF%2d8";
			*/
			/* t3 - time period (D=days, W=weeks, M=months, Y=years)
			   p3 - number of time periods between each recurrence */
			$href = $GLOBALS['mosConfig_live_site']."/index.php?option=com_myipn&amp;task=pay";
			$href = str_replace(" ", "%20", $href);
			$mailMsg = str_replace("{paypal_url}",$config->stringEncode($href), $mailMsg);
			$AdmMailMsg = str_replace("{paypal_url}",$config->stringEncode($href), $AdmMailMsg);
		}
		/*
		 * Send email to user
		 */
		mosMail($adminEmail2, $adminName2, $email, $mailTitle, "{$mailMsg} \n\n {$config->mailFooter}");
		/*
		 * Send email to admin
		 */
		if($config->mailFeesAdm){
			$query = "SELECT email, name"
				. "\n FROM #__users"
				. "\n WHERE gid = 25"
				. "\n AND sendemail = 1"
				;
			$database->setQuery($query);
			if(!$database->query()) {
				echo $database->stderr(true);
			}
			$adminRows = $database->loadObjectList();
			if ($database->getErrorNum())
				echo $database->stderr();

    		$AdmMailMsg .= $config->mailFooter;
			foreach( $adminRows as $adminRow)
				mosMail($adminRow->email, $adminRow->name, $adminRow->email, $AdmMailTitle, $AdmMailMsg);
		}
    }
    /*
     * replacing markers
     */
    function replaceMarkers($string) {
		global $config;
		$string = $this->sobi->replaceMarkers($string);
		$string = str_replace("{total}",$this->fees, $string);
		$string = str_replace("{bank_data}",str_replace("<br />","",$config->stringEncode($config->bankData)), $string);
		$string = str_replace("{bank_data}",str_replace("<BR>","",$config->stringEncode($config->bankData)), $string);
    	return $string;
    }
}
?>
